import Vuex from 'vuex'

// 首次从本地存储获取 购物车信息
const car = JSON.parse(localStorage.getItem('car') || '[]');

// 创建 vuex 实例
const vuexStore = {
    state: {
        commodity: 0,    // 购买数量
        // { id: 商品id, count: 商品数量, price: 商品价格, selected：true 是否选择 }
        shopcar: car     // 购物车信息
    },
    mutations: {
        modiCommodity(state, val) {
            let tmp = state.commodity + val;
            if (tmp < 0) tmp = 0;
            state.commodity = tmp;
        },

        // 添加到购物车
        addToShopCar(state, commodity) {
            // console.log(commodity);
            let findItem = state.shopcar.find(item => {
                if (item.id === commodity.id) 
                    return true;
            })

            console.log(findItem);
            if (findItem === undefined) {
                // 未找到，新增
                state.shopcar.push(commodity);                
            }
            else {
                // 找到，更新数量
                findItem.count += commodity.count;
            }


            localStorage.setItem('car', JSON.stringify(state.shopcar));
        },
        // 更新购物车商品信息
        updateShopCar(state, commodity) {
            let findItem = state.shopcar.find(item => {
                if (item.id === commodity.id) {
                    return true;
                }
            })

            
            if (findItem != undefined) {
                // 更新数量
                findItem.count = commodity.count;                
            }
            console.log(findItem)
            console.log(state.shopcar)
            localStorage.setItem('car', JSON.stringify(state.shopcar));
        },

        updateSelected(state, info) {
            state.shopcar.some(item => {
                if (item.id === info.id) {
                    item.selected = info.selected;
                }
            })

            localStorage.setItem('car', JSON.stringify(state.shopcar));
        },
        
        // 从购物车删除商品信息
        remFromShopCar(state, id) {
            let findItem = state.shopcar.find((item, i) => {
                if (item.id === id) {
                    state.shopcar.splice(i, 1); // 删除数据
                    return true;
                }
            })
            
            localStorage.setItem('car', JSON.stringify(state.shopcar));
        },

        
    },
    getters: {
        // 获取商品总数量
        getTotalCount(state) {            
            let num = 0;
            state.shopcar.forEach(item => {
                num += item.count;                
            })

            return num;
        },
        // 获取购物车id列表
        getIds(state) {
            let ids = [];
            state.shopcar.forEach(item => {
                ids.push(item.id);
            })

            let str = ids.join(",");

            // console.log(str);
            return str;
        },
        // 获取商品数量信息
        getGoodsCount(state) {
            var ret = {}

            state.shopcar.forEach(item => {
                ret[item.id] = item.count;
            })

            return ret;
        },
        // 获取选择状态
        getSelected(state) {
            let goodsSelected = {};
            state.shopcar.forEach(item => {
                goodsSelected[item.id] = item.selected;
            })

            return goodsSelected;
        },
        // 获取最后的商品统计信息
        getGoodsStatic(state) {
            let stat = {
                count: 0,
                price: 0
            }
            state.shopcar.forEach(item => {
                if (item.selected) {
                    stat.count += item.count;
                    stat.price += item.count * item.price;
                }
            })

            return stat;
        }
    }
}


export default vuexStore;